package com.quick.develop.flowable.controller.definition;

import com.quick.develop.framework.common.page.PageResult;
import com.quick.develop.framework.common.page.R;
import com.quick.develop.framework.utils.io.IoUtils;
import com.quick.develop.flowable.application.IBpmModelApplication;
import com.quick.develop.flowable.domain.model.*;
import com.quick.develop.flowable.framework.convert.model.BpmModelConvert;
import com.quick.develop.flowable.service.definition.IBpmModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;

/**
 * @author junyuan.liu
 * @description: 
 * @date 2022/4/21 18:53
 */
@Api(tags = "管理后台 - 流程模型")
@RestController
@RequestMapping("/model")
@Validated
public class BpmModelController  {
    @Resource
    private IBpmModelApplication bpmModelApplication;
    
    @Resource
    private IBpmModelService modelService;

    @GetMapping("/page")
    @ApiOperation(value = "获得模型分页")
    public R<PageResult<BpmModelPageItemRespVO>> getModelPage(BpmModelPageReqVO pageVO){
        return R.ok(modelService.getModelPage(pageVO));
    }

    @PostMapping("/create")
    @ApiOperation(value = "新建模型")
    public R<String> createModel(@Valid @RequestBody BpmModelCreateReqVO createRetVO){
        return R.ok(modelService.createModel(createRetVO,null));

    }

    @PostMapping("/import")
    @ApiOperation(value = "导入模型")
    public R<String> importModel(@Valid BpmModeImportReqVO importReqVO)throws IOException {
        BpmModelCreateReqVO createReqVO = BpmModelConvert.INSTANCE.convert(importReqVO);
        // 读取文件
        String bpmnXml = IoUtils.readUtf8(importReqVO.getBpmnFile().getInputStream(), false);
        return R.ok(modelService.createModel(createReqVO, bpmnXml));
    }

    @GetMapping("/get")
    @ApiOperation("获得模型")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    public R<BpmModelRespVO> getModel(@RequestParam("id") String id){
        BpmModelRespVO model = modelService.getModel(id);
        return R.ok(model);
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改模型")
    public R<Boolean> updateModel(@Valid @RequestBody BpmModelUpdateReqVO modelVO){
        modelService.updateModel(modelVO);
        return R.ok(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除模型")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    public R<Boolean> deleteModel(@RequestParam("id") String id){
        modelService.deleteModel(id);
        return R.ok(true);
    }

    @PostMapping("/deploy")
    @ApiOperation(value = "部署模型-流程发布")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    public R<Boolean> deployModel(@RequestParam("id") String id){
        modelService.deployModel(id);
        return R.ok(true);
    }

    @GetMapping("/get-xml")
    @ApiOperation("获得模型XML")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    public R<String> getBpmnXmlByModelId(@RequestParam("id") String id){
        return R.ok(modelService.getBpmnXmlByModelId(id));
    }

    @PutMapping("/update-state")
    @ApiOperation(value = "修改模型的状态", notes = "实际更新的部署的流程定义的状态")
    public R<?> updateModelState(@Valid @RequestBody BpmModelUpdateStateReqVO reqVO){
        modelService.updateModelState(reqVO);
        return R.ok();
    }

    @GetMapping("/get-model-elements/{key}")
    @ApiOperation(value = "获取流程图节点信息", notes = "只获取任务、调用子流程类型")
    public R<?> getModelElementsByProcessDefinitionKey(@PathVariable("key") String key) {
        return R.ok(bpmModelApplication.getModelElementsByProcessDefinitionKey(key));
    }
    
    @GetMapping("/get-model-elements-id/{id}")
    @ApiOperation(value = "获取流程图节点信息", notes = "只获取任务、调用子流程类型")
    public R<?> getModelElementsByProcessDefinitionId(@PathVariable("id") String id) {
        return R.ok(bpmModelApplication.getModelElementsByProcessDefinitionId(id));
    }

}
